package com.qianfeng.bob.xiexiaobo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.qianfeng.bob.xiexiaobo.R;
import com.qianfeng.bob.xiexiaobo.adapter.MatchContentAdapter;
import com.qianfeng.bob.xiexiaobo.adapter.SpacesItemDecoration;
import com.qianfeng.bob.xiexiaobo.bean.HomeFragmentDatas;
import com.qianfeng.bob.xiexiaobo.config.NetConfig;
import com.qianfeng.bob.xiexiaobo.utils.CommonUtils;
import com.qianfeng.bob.xiexiaobo.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Bob on 2015/12/16.
 */
public class MatchContentFragment extends  BaseFragment{
    private List<HomeFragmentDatas.DataEntity.ItemsEntity> datas;
    private List<Integer> heights;
    private RecyclerView recyclerView;
    private MatchContentAdapter adatper;

    private RecyclerView mRecycleView;
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view  = inflater.inflate(R.layout.fragment_match,container,false);
        initView(view);
        return view ;
    }

    private void initView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.cv_macthfragment_test);

        initData();


    }




    private void initData(){
        datas = new ArrayList<>();
       /* datas = new ArrayList<String>();
        for(int i = 'A'; i < 'z'; i++){
            datas.add(""+ (char)i);
        }*/
        getString(NetConfig.HOMEFRAGMENT_DATAS + 6, new StringCallBack() {
            @Override
            public void getString(String content) {
                Gson gson = new Gson();
                HomeFragmentDatas data = gson.fromJson(content,HomeFragmentDatas.class);
                getRandomHeight(datas);
                datas.addAll(data.getData().getItems());
                getRandomHeight(datas);
                adatper = new MatchContentAdapter(datas, getActivity(), heights, recyclerView);

                //  recyclerView.setLayoutManager(new LinearLayoutManager(this));//设置布局管理器
                //  recyclerView.setLayoutManager(new GridLayoutManager(this, 4));//4列
                recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));//1列，如果StaggeredGridLayoutManager.Horizontal，那么前面的参数代表几行
                recyclerView.addItemDecoration(new SpacesItemDecoration(10));//设置分割线
                recyclerView.setHasFixedSize(true);//固定RecyclerView的大小
                recyclerView.setItemAnimator(new DefaultItemAnimator());//为这个RecyclerView设置动画管理

                recyclerView.setAdapter(adatper);
                adatper.notifyDataSetChanged();

            }

            @Override
            public void getStringField() {
                ToastUtils.showLongTimeToast("网络连接失败请检查网络",getActivity());
            }
        });
    }

    private void getRandomHeight(List<HomeFragmentDatas.DataEntity.ItemsEntity> datas){//得到随机item的高度
        int picWidth = CommonUtils.getScreenWidth(getActivity())/2-20;
        Random random = new Random();
        heights = new ArrayList<Integer>();
        for (int i = 0; i < datas.size(); i++) {
            System.out.println(datas.get(i).getHeight());
            float sampleSize = (Integer.parseInt(datas.get(i).getWidth()))/picWidth;
            float hei = Integer.parseInt(datas.get(i).getHeight())/sampleSize;
            heights.add((int) hei-random.nextInt(60));
        }
    }


}
